// MailNoter - A capture tool for emails

// Copyright (C) 2009 - MailNoter

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//

#include "stdafx.h"
#include "Utils.h"
#include <CommCtrl.h>
#include <cctype>
#include <algorithm>

CUtils::CUtils(void)
{
}

CUtils::~CUtils(void)
{
}

WORD CUtils::HotKeyControl2HotKey(WORD hk)
{
    UINT flags = 0;
    if (HIBYTE(hk) & HOTKEYF_ALT)
        flags |= MOD_ALT;
    if (HIBYTE(hk) & HOTKEYF_SHIFT)
        flags |= MOD_SHIFT;
    if (HIBYTE(hk) & HOTKEYF_EXT)
        flags |= MOD_WIN;
    if (HIBYTE(hk) & HOTKEYF_CONTROL)
        flags |= MOD_CONTROL;
    return MAKEWORD(LOBYTE(hk), flags);
}

WORD CUtils::HotKey2HotKeyControl(WORD hk)
{
    UINT flags = 0;
    if (HIBYTE(hk) & MOD_ALT)
        flags |= HOTKEYF_ALT;
    if (HIBYTE(hk) & MOD_SHIFT)
        flags |= HOTKEYF_SHIFT;
    if (HIBYTE(hk) & MOD_WIN)
        flags |= HOTKEYF_EXT;
    if (HIBYTE(hk) & MOD_CONTROL)
        flags |= HOTKEYF_CONTROL;
    return MAKEWORD(LOBYTE(hk), flags);
}


const char * CUtils::stristr(const char *haystack, const char *needle)
{
    if (!*needle)
    {
        return haystack;
    }
    for ( ; *haystack; ++haystack)
    {
        if (toupper(*haystack) == toupper(*needle))
        {
            // Matched starting char -- loop through remaining chars.
            const char *h, *n;
            for (h = haystack, n = needle; *h && *n; ++h, ++n)
            {
                if (toupper(*h) != toupper(*n))
                {
                    break;
                }
            }
            if (!*n) // matched all of 'needle' to null termination
            {
                return haystack; // return the start of the match
            }
        }
    }
    return 0;
}

bool CUtils::IsImage(std::wstring path)
{
    // find the file extension
    size_t dotPos = path.find_last_of('.');
    if (dotPos != std::string::npos)
    {
        std::wstring extension = path.substr(dotPos);
        transform(extension.begin(), extension.end(), extension.begin(), tolower);
        if (extension.compare(_T(".jpg")) == 0)
            return true;
        if (extension.compare(_T(".jpeg")) == 0)
            return true;
        if (extension.compare(_T(".gif")) == 0)
            return true;
        if (extension.compare(_T(".png")) == 0)
            return true;
    }
    return false;
}

std::wstring CUtils::GetFileUrl(std::wstring path)
{
    std::wstring url = path;
    std::replace( url.begin(), url.end(), '\\', '/' );
    url = _T("file://") + url;

    return url;
}
